﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Aliyeye.Car.Net.UI.PopupDialog;
using Aliyeye.Car.Net.Utility;
using Aliyeye.Car.Net.UI.Common;
using System.IO;

namespace Aliyeye.Car.Net.UI.CarModel
{
    public partial class CarsInfoNew : Aliyeye.Car.Net.UI.BaseForm
    {

        CarSvc.SoapHeaderClass sopaHeader = new Aliyeye.Car.Net.UI.CarSvc.SoapHeaderClass();
        Base_DepartmentSvc.SoapHeaderClass sopaHeaderDepart = new Base_DepartmentSvc.SoapHeaderClass();

        private bool isDriver = false;
        private int CarID = 0;
        private int driverID = 0;
        private string driverName = "";
        bool boolresult = false;

        public CarsInfoNew()
        {
            InitializeComponent();
        }

        public CarsInfoNew(bool isDri)
        {
            InitializeComponent();
            isDriver = isDri;
        }

        public CarsInfoNew(bool isDri, int id)
        {
            InitializeComponent();
            isDriver = isDri;
            CarID = id;
            this.Text = "车辆信息";
        }

        private void BindInfo()
        {
            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            if (CarID != 0)
            {
                txtSendMile.Visible = false;
                lblsendmile.Visible = false;
            }

            CarSvc.Car model = new Aliyeye.Car.Net.UI.CarSvc.Car();
            ServiceManipulator.SafeAsyncExecution<CarSvc.CarServiceSoapClient>(
                proxy => proxy.GetModel(sopaHeader, CarID),
                result =>
                {
                    if (result.Error == null)
                    {
                        model = (CarSvc.Car)result.Result;
                        if (model != null)
                        {
                            this.txtBuyPrice.Text = model.BuyPrice.ToString();
                            this.txtBuyTime.Value = model.BuyTime;
                            this.txtInsureTime.Value = model.InsureTime;
                            this.txtCartype.Text = model.Cartype;
                            this.txtCode.Text = model.Code.ToString();
                            this.txtColor.Text = model.Color.ToString();
                            this.txtExhaustVolume.Text = model.ExhaustVolume.ToString();
                            this.txtdipanCode.Text = model.dipanCode;
                            this.txtDrive.Text = model.Drive == true ? "带驾" : "自驾";
                            this.txtEndbridge.Value = model.Endbridge;
                            this.txtEndOpen.Value = model.EndOpen;
                            this.txtEndCare.Value = model.EndCare;
                            this.txtEndChenshui.Value = model.EndChenshui;
                            this.txtEndOpen.Value = model.EndOpen;
                            this.txtEndyunguan.Value = model.Endyunguan;
                            this.txtFactorCode.Text = model.FactorCode;
                            this.txtInsureEnd.Value = model.InsureEnd;
                            this.txtInsureTime.Value = model.InsureTime;
                            this.txtMotorCode.Text = model.MotorCode;
                            //model.Max = max;
                            this.txtOvertimePrie.Text = model.OvertimePrie.ToString();
                            this.txtOwners.Text = model.Owners;
                            this.txtOwnerTel.Text = model.OwnerTel;
                            this.txtRoadEnd.Value = model.RoadEnd;
                            this.txtRoadTime.Value = model.RoadTime;
                            this.txtSeat.Text = model.Seat;
                            this.textShangpaishui.Text = model.Shangpaishui.ToString();
                            this.txtStarbridge.Value = model.Starbridge;
                            this.txtStarCheshui.Text = model.StarCheshui.ToString();
                            this.txtStarMax.Text = model.StarMax.ToString();
                            this.txtStaryunguan.Value = model.Staryunguan;
                            this.txtTax.Text = model.Tax.ToString();
                            this.cbbBrand.Text = model.Brand;
                            //this.txtUserId.Text = getName(model.UserId);
                            this.dtCarTime.Value = model.FactoryOutDate;
                            this.dtWarrTime.Value = model.WarrTime;
                            this.txtMarks.Text = model.Marks;
                            this.txtTimeoutPrie.Text = model.TimeoutPrie.ToString();
                            this.txtSendMile.Text = model.SendMile.ToString();
                            this.txtNextMile.Text = model.NextMile.ToString();

                            //this.txtDayKm.Text = model.DayKm.ToString();
                            this.cbbDepart.SelectedValue = model.DepID;

                            this.txtSendMile.Visible = true;
                            this.lblsendmile.Visible = true;
                            this.txtdayrent.Text = model.DayRent.ToString();
                            this.txtMax.Text = model.Max.ToString();

                            this.txtUserId.Text = model.DriverName;
                            driverID = model.UserId;
                            driverName = model.DriverName;

                            if (model.IsSelf)
                                rdbIsSelf.Checked = true;
                            else
                                rdbNoIsSelf.Checked = true;
                            if (model.IsActive)
                                rdbActive.Checked = true;
                            else
                                rdbNoActive.Checked = true;


                            if (model.CarImage != null)
                            {
                                pictureBoxCar.Image = byteToBitmap(model.CarImage);
                                byteUser = model.CarImage;
                            }

                            this.txtDayDeposit.Text = model.DayDeposit.ToString();
                            this.txtDayLimitKM.Text = model.DayLimitKM.ToString();
                            this.txtMonthRent.Text = model.MonthRent.ToString();
                            this.txtMonthDeposit.Text = model.MonthDeposit.ToString();
                            this.txtMonthLimitKM.Text = model.MonthLimitKM.ToString();
                            this.txtYearRent.Text = model.YearRent.ToString();
                            this.txtYearDeposit.Text = model.YearDeposit.ToString();
                            this.txtYearLimitKM.Text = model.YearLimitKM.ToString();
                            this.txtAirportShuttleOneWay.Text = model.AirportShuttleOneWay.ToString();
                            this.txtAirportShuttleTwoWay.Text = model.AirportShuttleTwoWay.ToString();
                            this.txtChartered3Rent.Text = model.Chartered3Rent.ToString();
                            this.txtChartered8Rent.Text = model.Chartered8Rent.ToString();
                            this.txtOutdatedFee3.Text = model.OutdatedFee3.ToString();
                            this.txtOutdatedFee8.Text = model.OutdatedFee8.ToString();
                            this.txtLongJourneys1.Text = model.LongJourneys1.ToString();
                            this.txtLongJourneys2.Text = model.LongJourneys2.ToString();
                            this.txtLongJourneys3.Text = model.LongJourneys3.ToString();
                            this.txtCommissionRatio.Text = model.CommissionRatio.ToString().Trim() == "" ? "0" : model.CommissionRatio.ToString();

                        }
                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                },
                this
            );

            //AccountsUsersSvc.Accounts_Users AccountsUserModel = new Aliyeye.Car.Net.UI.AccountsUsersSvc.Accounts_Users();
            //AccountsUsersSvc.SoapHeaderClass sopaHeadercustomer = new Aliyeye.Car.Net.UI.AccountsUsersSvc.SoapHeaderClass();
            //ServiceManipulator.SafeAsyncExecution<AccountsUsersSvc.Accounts_UsersServiceSoapClient>(
            //    proxy => proxy.GetModel(sopaHeadercustomer, model.UserId),
            //    result =>
            //    {
            //        if (result.Error == null)
            //        {
            //            AccountsUserModel = (AccountsUsersSvc.Accounts_Users)result.Result;
            //            if (AccountsUserModel != null)
            //            {
            //                this.txtUserId.Text = AccountsUserModel.TrueName;

            //            }

            //            overlay.FadeOut();Cursor.Current = Cursors.Default;
            //        }
            //        else
            //        {
            //            PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
            //        }
            //    },
            //    this
            //);
        }

        public Bitmap byteToBitmap(byte[] imageBytes)
        {
            if (imageBytes.Length == 0 || imageBytes.Length < 0)
            {
                return null;
            }

            string imageType = Encoding.ASCII.GetString(imageBytes, 20, 12);
            MemoryStream stream;
            if (imageType == "Bitmap Image")
            {
                //如果数据库中保存的是BMP图形则需要去掉OLE头
                stream = new MemoryStream(imageBytes, 78, imageBytes.Length - 78);
            }
            else
            {
                stream = new MemoryStream(imageBytes, 0, imageBytes.Length);
            }

            Bitmap bmp = new Bitmap(stream);

            return bmp;
        }

        private void AddCars_Load(object sender, EventArgs e)
        {
            BindComboBoxInfo();
            if (CarID != 0)
            {
                BindInfo();
                DisableControl();
            }
        }

        private void BindComboBoxInfo()
        {
            this.txtDrive.SelectedIndex = 0;

            //txtCartype 车型
            //txtSeat  座位数，型号
            //cbbBrand  厂牌

            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            ArchivesSvc.SoapHeaderClass sopaHeader = new ArchivesSvc.SoapHeaderClass();

            DataSet dsCarType = null;
            ServiceManipulator.SafeAsyncExecution<ArchivesSvc.ArchivesServiceSoapClient>(
                proxy => proxy.ComBoxCar(sopaHeader),
                result =>
                {
                    if (result.Error == null)
                    {
                        dsCarType = (DataSet)result.Result;
                        txtCartype.DisplayMember = "Namevalue";
                        txtCartype.ValueMember = "Id";
                        txtCartype.DataSource = dsCarType.Tables[0];
                        txtCartype.SelectedIndex = 0;

                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                },
                this
            );

            DataSet dsCarSeat = null;
            ServiceManipulator.SafeAsyncExecution<ArchivesSvc.ArchivesServiceSoapClient>(
                proxy => proxy.ComBoxSit(sopaHeader),
                result =>
                {
                    if (result.Error == null)
                    {
                        dsCarSeat = (DataSet)result.Result;
                        txtSeat.DisplayMember = "Namevalue";
                        txtSeat.ValueMember = "Id";
                        txtSeat.DataSource = dsCarSeat.Tables[0];
                        txtSeat.SelectedIndex = 0;

                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                },
                this
            );

            DataSet dsBrand = null;
            ServiceManipulator.SafeAsyncExecution<ArchivesSvc.ArchivesServiceSoapClient>(
                proxy => proxy.ComBoxBrand(sopaHeader),
                result =>
                {
                    if (result.Error == null)
                    {
                        dsBrand = (DataSet)result.Result;
                        if (dsBrand != null && dsBrand.Tables[0].Rows.Count > 0)
                        {
                            cbbBrand.DisplayMember = "Namevalue";
                            cbbBrand.ValueMember = "Id";
                            cbbBrand.DataSource = dsBrand.Tables[0];
                            cbbBrand.SelectedIndex = 0;
                        }
                        overlay.FadeOut();Cursor.Current = Cursors.Default;

                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                },
                this
            );


            DataSet dsColor = null;
            ServiceManipulator.SafeAsyncExecution<ArchivesSvc.ArchivesServiceSoapClient>(
                proxy => proxy.ComBoxColor(sopaHeader),
                result =>
                {
                    if (result.Error == null)
                    {
                        dsColor = (DataSet)result.Result;
                        if (dsColor != null && dsColor.Tables[0].Rows.Count > 0)
                        {
                            txtColor.DisplayMember = "Namevalue";
                            txtColor.ValueMember = "Id";
                            txtColor.DataSource = dsColor.Tables[0];
                            txtColor.SelectedIndex = 0;
                        }
                        overlay.FadeOut();Cursor.Current = Cursors.Default;

                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                },
                this
            );

            DataSet dtDepart = null;
            ServiceManipulator.SafeAsyncExecution<Base_DepartmentSvc.Base_DepartmentServiceSoapClient>(
                proxy => proxy.GetList(sopaHeaderDepart, ""),
                result =>
                {
                    if (result.Error == null)
                    {
                        dtDepart = (DataSet)result.Result;
                        cbbDepart.DisplayMember = "ClassName";
                        cbbDepart.ValueMember = "ClassID";
                        cbbDepart.DataSource = dtDepart.Tables[0];
                        cbbDepart.SelectedIndex = 0;

                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                },
                this
            );
        }


        private void DisableControl()
        {
            foreach (Control cont in this.tabPage1.Controls)
            {
                cont.Enabled = false;
            }

            foreach (Control cont in this.tabPage2.Controls)
            {
                cont.Enabled = false;
            }

            foreach (Control cont in this.tabPage3.Controls)
            {
                cont.Enabled = false;
            }

            foreach (Control cont in this.tabPage4.Controls)
            {
                cont.Enabled = false;
            }
        }

        //取消
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private string picPathUser = "";
        MemoryStream mStreamUser = null;
        byte[] byteUser = new byte[0];
        bool isUpdateForUser = false;

        private void btnModify_Click(object sender, EventArgs e)
        {
            AddCarsNew AddCarsForm = new AddCarsNew(isDriver, Convert.ToInt32(CarID));
            AddCarsForm.ShowDialog();

            this.Close();
        }


    }
}
